package com.example.service.authcenter;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.example.dao.authcenter.prd.TargetSystemOrganizationDao;
import com.example.entity.authcenter.SystemOrganization;
import com.example.entity.authcenter.SystemOrganizationExportDTO;
import com.example.entity.authcenter.cscec5b.ExternalSystemOrganizationDTO;
import com.google.common.collect.Lists;
import com.google.common.reflect.Reflection;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * XXX
 *
 * @author : jamesfu
 * @date : 9/7/2019
 * @time : 3:13 PM
 */
@Service
public class SystemOrganizationService {
    @Autowired
    private TargetSystemOrganizationDao targetSystemOrganizationDao;

    private List<ExternalSystemOrganizationDTO> allCecec5bUnitList;

    public void exportToExcel(Integer parentSysNo) throws IOException {

        List<SystemOrganizationExportDTO> resultList = exportLevelByLevel(parentSysNo);
        TemplateExportParams params = new TemplateExportParams(
                "templates/excel/云筑智联中建五局组织机构编码表.xls");
        Map<String, Object> map = new HashMap<>();
        map.put("list", resultList);

        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        File savefile = new File("/Users/jamesfu/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("/Users/jamesfu/云筑智联中建五局组织机构编码表.xls");
        workbook.write(fos);
        fos.close();
    }

    private List<SystemOrganizationExportDTO> exportLevelByLevel(Integer parentSysNo) {
        List<SystemOrganizationExportDTO> resultList = new ArrayList<>();
        int level = 1;
        exportByParent(parentSysNo, resultList, level);

        return resultList;
    }

    private void exportByParent(Integer parentSysNo, List<SystemOrganizationExportDTO> resultList, int level) {
        List<SystemOrganization> subList = getSubOrganizationList(parentSysNo, null);
        if (subList != null && subList.size() > 0) {
            for (SystemOrganization systemOrganization : subList) {
                SystemOrganizationExportDTO dto = buildExportDTO(systemOrganization, level);
                resultList.add(dto);
                exportByParent(systemOrganization.getSysNo(), resultList, level + 1);
            }
        }
    }

    private List<SystemOrganization> getSubOrganizationList(Integer parentSysNo, Integer sysNo) {

        SystemOrganization systemOrganizationExample = new SystemOrganization();
        systemOrganizationExample.setParentSysNo(parentSysNo);
        if (sysNo != null) {
            systemOrganizationExample.setSysNo(sysNo);
        }
        List<SystemOrganization> systemOrganizationList = targetSystemOrganizationDao.findAll(Example.of(systemOrganizationExample), new Sort(Sort.Direction.ASC, "SortIndex"));

        return systemOrganizationList;
    }

    private SystemOrganizationExportDTO buildExportDTO(SystemOrganization systemOrganization, int level) {
        SystemOrganizationExportDTO dto = new SystemOrganizationExportDTO();
        dto.setOrganizationCode(systemOrganization.getOrganizationCode());
        //根据level反射设置属性
        try {
            Field organizationNameField = dto.getClass().getDeclaredField("organizationName" + level);
            organizationNameField.setAccessible(true);
            organizationNameField.set(dto, systemOrganization.getOrganizationName());
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }

        //根据文本匹配度推荐相关的组织

        return dto;
    }

    private List<ExternalSystemOrganizationDTO> recommendCscec5bOrganization(SystemOrganization systemOrganization, int level) {
        return Collections.emptyList();
    }
}
